/*
 * @Author: chengjiang
 * @Date: 2024-06-08 17:05:35
 * @Description: 
 */
// 每一个存储的模块，命名规则use开头，store结尾
import { defineStore } from 'pinia'

type Custgold = {
    date: string,
    priceData: Map<string, string>,
    status: string,
}

export const useCustomerInfoStore = defineStore({
    id: 'customerInfo', // 必须指明唯一的pinia仓库的id
    state: () => {
        const custgold: Custgold = {
            date: '',
            priceData: new Map(),
            status: '',
        }
        return {
            custgold,
            ws: null as WebSocket | null,
        }
    },
    getters: {
        doubleCount: (state) => state
    },
    actions: {
        // 链接socket
        connectSocket(){
            this.ws = new WebSocket('wss://www.idjyj.com/web/v1/ws')
        },
        freshListener() {
            // 拦截socket
            if (this.ws) {
              console.log('监听socket')
              this.ws.onmessage = this.handlerSocketData
            }
          },
          // 定时请求http数据：
          getHttpData() {
            // 获取http数据
            // console.log('获取http数据')
            // this.custgold.date = '2024-06-08 17:05:35'
            // this.custgold.priceData = {
            //   黄金: '1.0',
            //   白银: '2.0',
            //   Au750: '3.0',
            //  铂金: '4.0',
          },
          // 监听socket   
          handlerSocketData(event: any) {
            const sortName: string[] = [
                '黄金',
                '白银',
                'Au750',
                '铂金',
                '钯金',
              ]
            const ws_data = JSON.parse(event.data)
            // console.log('通过拦截得到的数据：', ws_data)
            // test = {
            //   date: '',
            //   priceData: {}, // items
            //   status: '开盘',
            // }
            // 封装:
            const res: Custgold = {
                date:        ws_data.date,
                priceData:   ws_data.priceData,
                status:      ws_data.status
            }
            this.custgold = res
            // console.log('通过拦截得到的数据：', res)
          }
    },
})
